package com.tuanzi.loan.log.service.impl;

import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.util.SpringContextHolder;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.log.entity.ErrorLogStatus;
import com.tuanzi.loan.log.repository.ErrorLogRepository;
import com.tuanzi.loan.log.service.ErrorLogService;
import com.tuanzi.loan.log.vo.request.ErrorLogSearchRequest;
import com.tuanzi.loan.log.vo.response.ErrorLogDetailResponse;
import com.tuanzi.loan.log.vo.response.ErrorLogSearchResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * 异常日志业务类接口实现类
 * @author JinRuiyang 2017/11/21
 */
@Service
@Transactional
public class ErrorLogServiceImpl implements ErrorLogService {

    @Autowired
    private ErrorLogRepository errorLogRepository;

    /**
     * 查询异常日志详情
     *
     * @param id 异常日志
     * @return 异常日志详情
     */
    @Override
    public ErrorLogDetailResponse findById(Long id) {
        if (id != null)
            return errorLogRepository.findById(id);
        else
            throw new NullPointerException("method arg:id is null");
    }

    /**
     * 根据异常日志查询请求获取日志列表
     *
     * @param pageSearcher 日志查询请求
     * @return 异常日志列表
     */
    @Override
    public Page<ErrorLogSearchResponse> list(PageSearcher<ErrorLogSearchRequest> pageSearcher) {
        if (pageSearcher != null && pageSearcher.getSearchCondition() != null && pageSearcher.getPageable() != null){
            return errorLogRepository.list(pageSearcher.getSearchCondition(),pageSearcher.getPageable());
        }
        else{
            throw new NullPointerException("method arg:pageSearcher is null or invalid");
        }
    }

    /**
     * 获取异常日志表中的项目名称列表
     *
     * @return 项目名称列表
     */
    @Override
    public List<String> getAllProjectName() {
        List<String> projectNames = errorLogRepository.getProjectName();
        if (projectNames != null)
            return projectNames;
        else
            return Collections.emptyList();
    }

    /**
     * 批量标记异常日志的处理状态 state: 1 -> 0
     *
     * @param exceptions 异常信息集合，批量更新状态
     */
    @Override
    public void batchSetDone(List<ErrorLogSearchResponse> exceptions) {
        if (exceptions == null)
            throw new NullPointerException("method arg:exceptions is null");
        errorLogRepository.setStatus(exceptions,ErrorLogStatus.CHECKED.getValue());
    }

    /**
     * 标记异常日志的处理状态 state: 1 -> 0
     *
     * @param exception 异常信息，更新状态
     */
    @Override
    public void setDone(ErrorLogSearchResponse exception) {
        errorLogRepository.setStatus(exception, ErrorLogStatus.CHECKED.getValue());
    }
}
